package com.bdqn.controller;

import com.bdqn.dto.ApiResult;
import com.bdqn.entity.DataModel;
import com.bdqn.entity.Goods;
import com.bdqn.entity.Parameters;

import com.bdqn.entity.Staff;
import com.bdqn.mapper.StaffManageMapper;
import com.bdqn.service.StaffManageService;
import com.bdqn.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
public class StaffManageController {
    @Autowired
    StaffManageService service;
    @Autowired
    StaffManageMapper mapper;



    @RequestMapping(value = "StaffInfo")
    public DataModel<Staff> getGoodsInfo(Parameters parameters) {
        parameters.setPage((parameters.getPage() - 1) * parameters.getLimit());
        DataModel<Staff> dataModel = new DataModel<>();
        dataModel.setCode(0);
        dataModel.setMsg("");
        dataModel.setCount(service.getCount(parameters));
        dataModel.setData(service.getStaffInfo(parameters));
        return dataModel;
    }

    @RequestMapping(value = "addStaff")
    public int addGoodsInfo(Staff staff) {
        return service.addStaffInfo(staff);
    }

    @RequestMapping(value = "delStaff")
    public int delGoodsInfo(int id) {
        return service.delStaffInfo(id);
    }

    @RequestMapping(value = "updateStaff")
    public int updateGoodsInfo(Staff staff) {
        return service.updateStaffInfo(staff);
    }

    @RequestMapping(value = "updatePwd")
    public ApiResult updatePwd(HttpServletRequest request,String oldPass,String newPwd) {
        String staffId=UserUtil.getUserName(request);
        if(oldPass==null || oldPass.isEmpty() ){
            return new ApiResult(500, "旧密码不能为空", null);
        }
        int num=mapper.getUserPwd(staffId,oldPass);

        if(num<=0){
            return new ApiResult(500, "旧密码验证错误", null);
        }
        if(newPwd==null || newPwd.isEmpty() ){
            return new ApiResult(500, "新密码不能为空", null);
        }
        int number=mapper.updatePwd(staffId,newPwd);
        if(number>0){
            return new ApiResult(200,"修改成功!",null);
        }
        return new ApiResult(500,"修改失败!",null);
    }

    @RequestMapping(value = "getStaff")
    public List<Staff> getStaff() {
        return service.getStaff();
    }
    @RequestMapping(value = "getUserName")
    public ApiResult getStaff(HttpServletRequest request) {
        return new ApiResult(200,"查询成功",mapper.getUserName(UserUtil.getUserName(request)).getStaffName());
    }




    @RequestMapping(value = "delGoods")
    public int delGoodsInfo(@RequestParam(value="arr[]") int[] arr){
        return service.delStaffInfoss(arr);
    }

    @RequestMapping(value = "login")
    public ApiResult Login(String staff_id, String staff_pwd) {
        if( staff_id==null || staff_id.isEmpty()){
            return new ApiResult(500, "用户名不能为空", null);
        }
        if( staff_pwd==null || staff_pwd.isEmpty()){
            return new ApiResult(500, "密码不能为空", null);
        }
        String userName = service.Login(staff_id, staff_pwd);
        String token = "";
        try (Jedis jedis = new Jedis("47.106.226.199", 6429)) {
            jedis.auth("666t01");
            if (userName.equals(staff_id)) {
                ///2、生成token
                String encodeString = staff_id + staff_pwd + System.currentTimeMillis();
                token = DigestUtils.md5DigestAsHex(encodeString.getBytes());

                //3、将token设置到redis，key为token  value为用户id
                jedis.setex(token, 60 * 20, userName);
                return new ApiResult(200, "登录成功", token);
            }
            return new ApiResult(500, "登录失败", null);
        } catch (NullPointerException pointerException) {
            return new ApiResult(500, "用户名或密码错误", null);
        }

    }


}
